/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLAttribute;
import org.mizartools.utility.xml.XMLElement;

public class Pattern implements IElement {

    public enum Kind { M, L, V, R, K, U, G, J,}
    public enum Constrkind { M, L, V, R, K, U, G, J,}

    public static final String NAME = "Pattern";

    private INode parentNode;
    private Kind kind = null;
    private Integer nr = null;
    private String aid = null;
    private Integer formatnr = null;
    private Constrkind constrkind = null;
    private Integer constrnr = null;
    private Boolean antonymic = null;
    private Integer relnr = null;
    private Integer redefnr = null;

    private Format format = null;
    private ArgTypes argTypes = null;
    private Visible visible = null;
    private Expansion expansion = null;

    private Pattern(INode parentNode){
        this.parentNode = parentNode;
    }

    public INode getParentNode() {
        return parentNode;
    }

    public static Pattern newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        Pattern pattern = new Pattern(parentNode);
        XMLElement xmlElement = xmlElementList.poll();
        if (!xmlElement.getName().equals(NAME)){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a " + NAME, xmlElementList);
        }
        for (XMLAttribute xmlAttribute : xmlElement.getXMLAttributeList()){
            if (xmlAttribute.getName().equals("kind")){
                if (pattern.kind == null){
                    if (Kind.valueOf(xmlAttribute.getValue()) != null) {
                        pattern.kind = Kind.valueOf(xmlAttribute.getValue());
                    } else 
                        throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("nr")){
                if (pattern.nr == null){
                    try {pattern.nr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("aid")){
                if (pattern.aid == null){
                    pattern.aid = xmlAttribute.getValue();
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("formatnr")){
                if (pattern.formatnr == null){
                    try {pattern.formatnr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("constrkind")){
                if (pattern.constrkind == null){
                    if (Constrkind.valueOf(xmlAttribute.getValue()) != null) {
                        pattern.constrkind = Constrkind.valueOf(xmlAttribute.getValue());
                    } else 
                        throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("constrnr")){
                if (pattern.constrnr == null){
                    try {pattern.constrnr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("antonymic")){
                if (pattern.antonymic == null){
                    try {pattern.antonymic = Boolean.parseBoolean(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("relnr")){
                if (pattern.relnr == null){
                    try {pattern.relnr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("redefnr")){
                if (pattern.redefnr == null){
                    try {pattern.redefnr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
                throw new ElementParseException(ElementParseException.NOT_VALID_ATTRIBUTE_ELEMENT, "XMLElement has not a valid attribute " + xmlAttribute.getName(), xmlElementList);
        }

        if (!xmlElement.isEmpty()){
            while (true){
                xmlElement = xmlElementList.peek();
                if (xmlElement == null){
                    throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
                }
                if (xmlElement.getName().equals(NAME) && xmlElement.isEndElement()){
                    xmlElement = xmlElementList.poll();
                    break;
                }
                if (xmlElement.getName().equals("Format")){
                    if (pattern.format == null) {
                        pattern.format = Format.newInstance(pattern, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Format> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("ArgTypes")){
                    if (pattern.argTypes == null) {
                        pattern.argTypes = ArgTypes.newInstance(pattern, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <ArgTypes> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("Visible")){
                    if (pattern.visible == null) {
                        pattern.visible = Visible.newInstance(pattern, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Visible> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("Expansion")){
                    if (pattern.expansion == null) {
                        pattern.expansion = Expansion.newInstance(pattern, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Expansion> in element " + NAME, xmlElementList);
                    }
                } else 
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
            }
        }

        return pattern;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        XMLElement xmlElement = new XMLElement(NAME);
        if (this.kind != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("kind", this.kind.name());
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.nr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("nr", this.nr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.aid != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("aid", this.aid);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.formatnr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("formatnr", this.formatnr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.constrkind != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("constrkind", this.constrkind.name());
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.constrnr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("constrnr", this.constrnr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.antonymic != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("antonymic", this.antonymic);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.relnr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("relnr", this.relnr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.redefnr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("redefnr", this.redefnr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        boolean isEmpty = !(format != null
         || argTypes != null || visible != null
         || expansion != null);
        xmlElement.setEmpty(isEmpty);
        xmlElementList.add(xmlElement);
        if (this.format != null){
            xmlElementList.addAll(this.format.getXMLElementList());
        }
        if (this.argTypes != null){
            xmlElementList.addAll(this.argTypes.getXMLElementList());
        }
        if (this.visible != null){
            xmlElementList.addAll(this.visible.getXMLElementList());
        }
        if (this.expansion != null){
            xmlElementList.addAll(this.expansion.getXMLElementList());
        }
        if (!isEmpty) { 
            xmlElement = new XMLElement(NAME);
            xmlElement.setEndElement(true);
            xmlElementList.add(xmlElement);
        }
        return xmlElementList;
    }

    public Kind getKind(){
        return kind;
    }

    public Integer getNr(){
        return nr;
    }

    public String getAid(){
        return aid;
    }

    public Integer getFormatnr(){
        return formatnr;
    }

    public Constrkind getConstrkind(){
        return constrkind;
    }

    public Integer getConstrnr(){
        return constrnr;
    }

    public Boolean getAntonymic(){
        return antonymic;
    }

    public Integer getRelnr(){
        return relnr;
    }

    public Integer getRedefnr(){
        return redefnr;
    }

    public Format getFormat(){
        return format;
    }

    public ArgTypes getArgTypes(){
        return argTypes;
    }

    public Visible getVisible(){
        return visible;
    }

    public Expansion getExpansion(){
        return expansion;
    }

	@Override
	public void accept(Visitor visitor) {
		if (visitor.visit(this)) {}
		visitor.endVisit(this);
	}

	@Override
	public String toString(){
		ToStringVisitor toStringVisitor = new ToStringVisitor();
		this.accept(toStringVisitor);
		return toStringVisitor.toString();
	}

}
